<?php
namespace app\admin\controller;
use cmf\controller\AdminBaseController;
use think\Db;

class ResendmailController extends AdminBaseController
{
	public function index()
	{
		$content = hook_one('admin_resendmail_index_view');
		if (!empty($content)) {
			return $content;
		}

		$where = ['id' => ['neq', 0]];
		/**搜索条件**/
		$uname = $this->request->param('uname');
		if ($uname) {
			$where['uname'] = ['like', "%$uname%"];
		}

		$resendmailList = Db::name('resendmail')
			->where($where)
			->order("id DESC")
			->paginate(50);

		$resendmailList->appends(['uname' => $uname]);
		// 获取分页显示
		$page = $resendmailList->render();

		$this->assign("page", $page);
		$this->assign("resendmailList", $resendmailList);
		return $this->fetch();
	}

	public function add()
	{
		return $this->fetch();
	}

	public function addPost()
	{
		if ($this->request->isPost()) {
			if (!empty($_POST['uname'])) {
				$_POST["create_time"] = date("Y-m-d H:i:s");
				$_POST["update_time"] = date("Y-m-d H:i:s");
				
				$result	= DB::name('resendmail')->insertGetId($_POST);
				if ($result !== false) {
						$this->success("添加成功！", url("resendmail/index"));
					} else {
						$this->error("添加失败！");
					}
			} else {
				$this->error("站点名称能为空！");
			}
		}
	}

	public function edit()
	{
		$content = hook_one('admin_resendmail_edit_view');

		if (!empty($content)) {
			return $content;
		}

		$id      = $this->request->param('id', 0, 'intval');
		$resendmail = DB::name('resendmail')->where(["id" => $id])->find();
		$this->assign($resendmail);
		return $this->fetch();
	}

	public function editPost()
	{
		if ($this->request->isPost()) {
			if (!empty($_POST['id'])) {
				$_POST["update_time"] = date("Y-m-d H:i:s");
				

				$result = DB::name('resendmail')->update($_POST);
				if ($result !== false) {
						$this->success("添加成功！", url("resendmail/index"));
					} else {
						$this->error("添加失败！");
					}
			} else {
				$this->error("ID不能为空！");
			}
		}
	}

	public function delete()
	{
		$id = $this->request->param('id', 0, 'intval');

		if (Db::name('resendmail')->delete($id) !== false) {
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	}
}
